/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;



import DTO.NGUOI;
import DTO.TRONGTAI;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 *
 * @author LUCKYSTARS
 */
public class TrongTaiDAO {
    Connect con =new Connect();
    Connection cont;
    public TrongTaiDAO()
    {
        cont= con.Connect("sa","123456");
    }
    
    public ResultSet Getdata()
    {
        String sqlGetdata = "select * from view_Trongtai";
        return con.GetdataTable(sqlGetdata);
    }
     public String GetMaFTenTrongTai(NGUOI n)
    {
          return con.GetMaDK("MA_TT", "view_Trongtai", "HO_TEN",n.getHoten());
    }
     
     public ResultsModel Getdata1()
     {
         String[] title = {"Mã Trọng Tài","Họ Tên","Ngày Sinh","Quốc Tịch"};
         Result rs = new Result();
         return rs.GetResult(cont,"select * from View_Trongtai", title);
     }
     
     public void Insert(TRONGTAI tt)
     {
          String sql  = "{call spThemTrongTai(?,?,?,?,?)}";
          ArrayList<String> a = new ArrayList();
          a.add("ThongBao");
          a.add(tt.getNguoi().getManguoi());
          a.add(tt.getNguoi().getHoten());
          SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
          String formattedDate = formatter.format(tt.getNguoi().getDate()); 
          a.add(formattedDate);
          a.add(tt.getNguoi().getQuoctich());
          con.Insert(sql, a, true);
     }
          
     
     public void Update(TRONGTAI tt)
      {
          String sql = "{call spSuaTrongTai(?,?,?,?)}";
          ArrayList<String> a = new ArrayList();
          a.add("thong bao");
          a.add(tt.getNguoi().getManguoi());
          a.add(tt.getNguoi().getHoten());
          SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
          String formattedDate = formatter.format(tt.getNguoi().getDate()); 
          a.add(formattedDate);
          a.add(tt.getNguoi().getQuoctich());
          
          con.Update(sql, a, true);
          
          
      }
      
      public void Delete(String MaTT)
      {
          String sql = "{call spXoaTrongTai(?)}";
          ArrayList<String> a = new ArrayList();
          a.add("Thong bao");
          a.add(MaTT);
          con.Update(sql,a, true);
      }
      
      public String LayMaCuoi()
      {
          String str="";
          try
          {
          String query = "select MA_TT from TRONGTAI order by MA_TT desc";
          ResultSet rs = null;
          rs = con.GetdataTable(query);
          rs.next();
          str = rs.getString(1);
          }
          catch(Exception ex)
          {}
          return str;
      }
}
